const lodash = require("lodash")
import { RuleDao } from '@/dao/ruleDao'
import { TaskDao } from '@/dao/taskDao'
import { UsageScope } from '@/entities/Constant'

async function startSpider(source, page) {
    try {
        let whereDict = {
            enable: 1,
            usageScope: UsageScope.Spider,
            sourceType: source.batch,
            platformId: source.platform,
        }
        let tasks = await TaskDao.fetchAll(whereDict)
        if (lodash.isEmpty(tasks)) {
            return { err: "没有采集任务：" + source.id }
        }
        let res = { err: null, data: null }
        for (let task of tasks) {
            let rule = await RuleDao.fetchById(task.ruleId)
            if (lodash.isEmpty(rule)) {
                res.err = "没有采集规则：" + task.name
                continue
            }
            let postFns = require('./' + rule.module)
            res = await postFns.goSpider(page, task, source, rule)
            if (!res.err) {
                break
            }
        }
        return res
    } catch (err) {
        return { err }
    }
}
export { startSpider }